package com.gtcom.sdk.utils;

import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.Environment;
import android.util.Log;

import com.gtcom.sdk.asr.GTRecognizer;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Date;


/**
 * 录音相关的线程
 */

public class RecordThread extends Thread {

    private static final String TAG = "RecordThread";

    //配置参数
    private static int sampleRateInHz = 16000;
    private static final int channelConfig = AudioFormat.CHANNEL_IN_MONO;
    private static final int audioFormat = AudioFormat.ENCODING_PCM_16BIT;

    private AudioRecord audioRecord;

    //标志位
    private boolean isRecording = false;

    int bufferSize;

    /**
     * 录音需要传入码率字段
     *
     * @param rateHz
     */
    public RecordThread() {
        bufferSize = AudioRecord.getMinBufferSize(sampleRateInHz, channelConfig, audioFormat);
        audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRateInHz, channelConfig, audioFormat, bufferSize);
    }


    @Override
    public void run() {
        try {
            Log.e(TAG, "RecordThread run...");
            byte[] buffer = new byte[bufferSize];
            audioRecord.startRecording();

            while (isRecording) {
                audioRecord.read(buffer, 0, buffer.length);
                // 将音频流发送到ASR服务中
                GTRecognizer.sendAudioStream(buffer);

                //保存到文件
                os.write(buffer);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void startRecord() {
        isRecording = true;
        createTempFile();
        start();
    }

    public void stopRecord() {
        try {
            Thread.sleep(50);
            isRecording = false;
            if (audioRecord != null) {
                audioRecord.stop();
                audioRecord.release();
                audioRecord = null;
                Log.e(TAG, " stopAsr... RecordThread releaseASR....");
            }

            os.close();
            com.gtcom.sdk.utils.PcmToWavUtil util = new com.gtcom.sdk.utils.PcmToWavUtil(sampleRateInHz, channelConfig, audioFormat);
            util.pcmToWav(pcmFile.getAbsolutePath(), pcmFile.getAbsolutePath()+".wav");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //保存音频文件
    File pcmFile;
    FileOutputStream os;

    public void createTempFile(){
        String externalPath = Environment.getExternalStorageDirectory().getAbsolutePath();
        File directory = new File(externalPath + "/" + "gtcom/voice");
        if (!directory.exists()) {
            directory.mkdirs();
            Log.d("sdkdemo", "Created directory");
        }
        pcmFile = new File(directory, "pcm_" + new Date().getTime());
        try {
            os = new FileOutputStream(pcmFile);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }


}
